//
//  InviteCoupon_friends.m
//  XJMCustomer
//  Created by 洗脚么_one on 2017/11/28.
//  Copyright © 2017年 Mr.hu. All rights reserved.

#import "InviteCoupon_friends.h"
#import "MyInvite_Friends.h"
#import "Invite_RuleIntroducecoupon.h"
#import <UShareUI/UShareUI.h>

@interface InviteCoupon_friends ()
@property (weak, nonatomic) IBOutlet UIImageView *image_QRCode;

@property (weak, nonatomic) IBOutlet UILabel *invite_money;
@property (weak, nonatomic) IBOutlet UILabel *invite_itemlabe;

@property (weak, nonatomic) IBOutlet UILabel *invite_timelab;
@property (weak, nonatomic) IBOutlet UIImageView *coupon_Headimage;
@property (nonatomic,strong)  NSString* thumbURL;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *Topbackground_conostaits;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *InviteFriends_Conostaits;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *MoneyBring_conostaits;

@end

@implementation InviteCoupon_friends

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([[XJToolClass iphoneType] isEqualToString:@"iPhone 5s"]) {
        self.Topbackground_conostaits.constant-=45;
        self.InviteFriends_Conostaits.constant-=10;
        self.MoneyBring_conostaits.constant-=10;
    }
    
    [self.navigationController setNavigationBarHidden:NO];
    [self addLeftBtn:[UIImage imageNamed:@"fanhui"] Action:@selector(backToPrevious)];
    self.title = @"邀请好友";
    [self addRightLabel:@"规则说明" Action:@selector(RuleIntroductMeth)];
    
    [XJHttpClass InviteFriendSFinishblock:^(id response, NSString *errStr) {
        
        if (errStr) {
//            [XJToolClass tipAnimationWith:errStr];
        }else{
            
            NSDictionary * dictdata = response[@"data"];
            
            if ([dictdata isKindOfClass:[NSDictionary class]]) {
                
                [self.image_QRCode sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",dictdata[@"qr_code"]]] placeholderImage:[UIImage imageNamed:@"DefaultQr_image"]];
                [self.coupon_Headimage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",dictdata[@""]]] placeholderImage:[UIImage imageNamed:@"set-quan"]];
                self.thumbURL = dictdata[@"registerUrl"]; //分享的URL
            }
        }
    }];
    
    //设置用户自定义的平台
    [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_WechatSession),
                                               @(UMSocialPlatformType_WechatTimeLine),
                                               @(UMSocialPlatformType_QQ),
                                               @(UMSocialPlatformType_Qzone),
                                               @(UMSocialPlatformType_Sina),]];
}

- (void)RuleIntroductMeth{
    
    Invite_RuleIntroducecoupon * inviterule = [[Invite_RuleIntroducecoupon alloc]init];
    [self.navigationController pushViewController:inviterule animated:YES];
}
     
- (IBAction)Share_awardmeth:(id)sender {
    
    MyInvite_Friends * invitefriend = [[MyInvite_Friends alloc]init];
    [self.navigationController pushViewController:invitefriend animated:YES];
}

- (IBAction)Invite_friendsMeth:(id)sender {
    
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        
        //创建网页内容对象
        NSString* thumbURL =  [NSString stringWithFormat:@"%@",self.thumbURL];
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"一技在手，轻松赚钱！" descr:[NSString stringWithFormat:@"加入喜脚么技师吧！随时接单选择上门服务，轻松赚钱"] thumImage:[UIImage imageNamed:@"108x108"]];
        //设置网页地址
        shareObject.webpageUrl = [NSString stringWithFormat:@"%@",self.thumbURL];
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
        //调用分享接口
        [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
            
            if (error) {
                UMSocialLogInfo(@"************Share fail with error %@*********",error);
            }else{
                if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                    UMSocialShareResponse *resp = data;
                    //分享结果消息
                    UMSocialLogInfo(@"response message is %@",resp.message);
                    //第三方原始返回的数据
                    UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                    
                }else{
                    UMSocialLogInfo(@"response data is %@",data);
                }
            }
            [self alertWithError:error];
        }];
    }];
}

- (void)alertWithError:(NSError *)error
{
    NSString *result = nil;
    if (!error) {
//        result = [NSString stringWithFormat:@"分享"];
    }
    else{
        NSMutableString *str = [NSMutableString string];
        if (error.userInfo) {
            for (NSString *key in error.userInfo) {
                [str appendFormat:@"%@ = %@\n", key, error.userInfo[key]];
            }
        }
        if (error) {
            result = [NSString stringWithFormat:@"Share fail with error code: %d\n%@",(int)error.code, str];
        }
        else{
            result = [NSString stringWithFormat:@"Share fail"];
        }
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                    message:result
                                                   delegate:nil
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil];
    [alert show];
}


@end
